import { defineConfig, loadEnv, ConfigEnv } from "vite";
import vue from "@vitejs/plugin-vue";
import { resolve } from "path";

export default defineConfig((mode: ConfigEnv) => {
  const env = loadEnv(mode.mode, process.cwd());
  // const env = loadEnv("production", process.cwd());
  return {
    lintOnSave: false,
    plugins: [vue()],
    resolve: {
      // 配置路径别名
      alias: {
        "@": resolve(__dirname, "src"),
      },
      // 配置文件扩展名
      extensions: [".ts", ".vue", ".js", ".jsx", ".tsx"], // 导入时想要省略的扩展名列表。
    },
    server: {
      port: +env.VITE_PORT,
      open: env.VITE_OPEN === "true",
      // 配置代理服务器
      proxy: {
        "/api": {
          target: "http://localhost:9000",
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/api/, ""),
        },
        "/dbjson": {
          target: "http://localhost:3000",
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/dbjson/, ""),
        },
      },
    },
  };
});
